#include <project.h>

/*-------------------------------------------------------------------------

***************** Modification History ***********************************

   @doc
   @module report3.c - QA Debug Report functions |
   @end
   
$Log: /SoftPay/SPK2SIM/Source/Main/report3.c $ 

**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/

#define VSREPT3_C

#include <aclconio.h>
#include <stdlib.h>
#include <stdio.h>
#include <applidl.h>
#include <svctxo.h>
#include <dbmgr.h>   
#include <formater.h>
#include <aclstr.h>
#include <aclutil.h>
#include <string.h>
#include <actutil.h>

/** /#define DEBUG_VSREPORT*/
#ifdef DEBUG_VSREPORT
#define DEBUG_95
#endif

#include <define.h>
#include <transact.h>
#include <report.h>
#include <cdt.h>
#include <cst.h>
#include <hdt.h>
#include <tblutil.h>
#include <tct.h>
#include <msg.h>
#include <msgfuncs.h>
#include <tran.h>
#include <ui.h>
#include <form.h>
#include <form2.h>
#include <spdebug.h>
#include <print.h>
#include <trt.h>
#include <confio.h>
#include <ztconf.h>
#include <mem.h>
#include <list.h>
#include <pscalls.h>
#include <mht.h>
#include <pct.h>
#include <lct.h>
#include <test.h>

extern char* pchGetMessage(long);
extern char* aszParamNames[];

char** TablesForQAReportList = aszParamNames;

#define TEMP_DAT_FILE_NAME "temp.dat"

/*-------------------------------------------------------------------------
    Function :    inQADebugReportOperation()
    Description :
    Parameters :
    Returns :
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      4   09/15/98 Page_K1    Use inGetReport2Template() instead of "rept"
-------------------------------------------------------------------------*/

int inQADebugReportOperation(TRANSACTION_OBJECT* pobTran) {
    int inResult;

    if (!fGetMegaPassword()) {
        vdDisplayMessageFromFile(TRANS_NOT_ALLWD_ERR_MSG, WARNING_MSG);
        return(VS_SUCCESS);
    }
    if (inGetPrinterType() == NOPRINTER && fGetEnablePrinter()) {
        vdDisplayMessageFromFile(NO_PRINTER_MSG, WARNING_MSG);
        return(VS_SUCCESS);
    }
    if (!fGetEnablePrinter())
        return(VS_SUCCESS);

    inGetReport2Template();

    if ((inResult = fGetPrintRecs(&obPrinter)) != VS_SUCCESS) {
        if (inResult == VS_ESCAPE)
            vdDisplayMessageFromFile(TXN_CANCELLED, ERROR_MSG);
        else
            vdDisplayMessageFromFile(TABLES_ERR_MSG, ERROR_MSG);
    }

    return(VS_SUCCESS);
}

/*                                                                    */
/**********************************************************************/
/**************************************************************************/
/*
 * Function : inGetPrintRecs () - Gets configuration records from
 *            vsXXX.dat file, extracts the fields, and prints the
 *            data to a report.
 * Input    : void.
 * Output   : VS_SUCCESS on success; VS_ERR on failure like write error,
 *             seek error into the file etc.
 */

VS_BOOL fGetPrintRecs(PRINTER_OBJECT* pobPrinter) {
    char szDataFile[FILENAME_SIZE + 1];
    int inFileNum = 0;
    int inResult;
    VS_BOOL fTempFile;
    char szChar[1 + 1];
    long lnCondition = 0L;

    /*print the Header lines*/
    vdSetReportDateAndTime();


    if (inPrintLines(pobPrinter, PARAM_HEADER_START, PARAM_HEADER_END, NO_BL_LINE, lnCondition) != VS_SUCCESS)
        return(VS_FALSE);

    if ((inResult = inGetHostTRTFile(pobPrinter)) != VS_SUCCESS) {
        if (inResult == VS_ESCAPE)
            return (VS_FALSE);
        else
            return(VS_FALSE);
    }

    if (inPrintLines(pobPrinter, PARAM_TABLE_HEADER_START, PARAM_TABLE_HEADER_END, NO_BL_LINE, lnCondition) != VS_SUCCESS)
        return(VS_FALSE);

    if ((inResult = inGetHostHFTFile(pobPrinter)) != VS_SUCCESS) {
        if (inResult == VS_ESCAPE)
            return (VS_FALSE);
        else
            return(VS_FALSE);
    }

    while (TablesForQAReportList[inFileNum][0]) {
        sprintf(szDataFile, "%s", TablesForQAReportList[inFileNum]);
        if ((inResult = inConfPrintRecs(pobPrinter, szDataFile, szDataFile)) != VS_SUCCESS) {
            if (inResult == VS_ESCAPE)
                return(VS_FALSE);
            else {
                if (inPrintLines(pobPrinter, PARAM_FORMFEED, PARAM_FORMFEED, NO_BL_LINE, lnCondition) != VS_SUCCESS)
                    return(VS_FALSE);
            }
        }

        /* print all merchant CDT, CST, MHT files */
        if ((strncmp(szDataFile, "CDT", 3) == 0) || (strncmp(szDataFile, "CST", 3) == 0) || (strncmp(szDataFile, "MIT", 3) == 0)) {
            /* change to switch */
            if (strncmp(szDataFile, "CDT", 3) == 0) {
                sprintf(szChar, "%s", "C");
                fTempFile = VS_TRUE;
            } else if (strncmp(szDataFile, "CST", 3) == 0) {
                sprintf(szChar, "%s", "S");
                fTempFile = VS_TRUE;
            } else if (strncmp(szDataFile, "MIT", 3) == 0) {
                sprintf(szChar, "%s", "M");         
                fTempFile = VS_TRUE;
            }

            if ((inResult = inPrintMerchTables(szDataFile, szChar, fTempFile)) != VS_SUCCESS) {
                if (inResult == VS_ESCAPE)
                    return(VS_FALSE);
                else {
                    if (inPrintLines(pobPrinter, PARAM_FORMFEED, PARAM_FORMFEED, NO_BL_LINE, lnCondition) != VS_SUCCESS)
                        return(VS_FALSE);
                }
            }
        }
        /* continue with other files */

        inFileNum++;
    }

    if (inPrintLines(pobPrinter, PARAM_FORMFEED, PARAM_FORMFEED, NO_BL_LINE, lnCondition) != VS_SUCCESS)
        return(VS_FALSE);


    return(VS_SUCCESS);
} /* End of fGetPrintRecs () */


/********************************************************************************************* 
* @func int | inPrintMerchTables | 
*       Prints merchant specific CDT, CST, and MHT information. 
*
* @parm char *  | pchDataFileName | 
*
* @rdesc Return values.
* @flag int    |  VS_ERR, VS_ESCAPE or VS_SUCCESS
* @end
**********************************************************************************************/

int inPrintMerchTables(char* pchDataFileName,char* pchCharName,VS_BOOL fTempFile) {
    int inMerch,
    inResult;
    char szDataFile[FILENAME_SIZE + 1];
    char szPrintName[FILENAME_SIZE + 1];
    long lnCondition = 0L;

    for (inMerch = 1; inMerch <= MAX_MERCHS; ++inMerch) {
        /* first time through, append file extension */ 
        if (inMerch == 1)
            strcat(pchDataFileName, ".DAT");
        /* save original default file, if one exists */
        if (fTempFile)
            rename(pchDataFileName, TEMP_DAT_FILE_NAME);

        sprintf(szDataFile, "%s%02d.DAT", pchCharName, inMerch);
        if (inChkFileExist(szDataFile) == VS_SUCCESS) {
            /* rename merchant file, ConfPrintRecs needs original name */
            rename(szDataFile, pchDataFileName);
            MEMCLR(szPrintName, sizeof(szPrintName));
            strncpy(szPrintName, szDataFile, 3);
            MEMCLR(szDataFile, sizeof(szDataFile));
            strncpy(szDataFile, pchDataFileName, 3);
            if ((inResult = inConfPrintRecs(&obPrinter, szDataFile, szPrintName)) != VS_SUCCESS) {
                inPrintLines(&obPrinter, PARAM_FORMFEED, PARAM_FORMFEED, NO_BL_LINE, lnCondition);
                if (inResult == VS_ESCAPE)
                    return(VS_ESCAPE);
                else
                    return(VS_ERR);
            }

            /* rename files back again */ 
            sprintf(szDataFile, "%s%02d.DAT", pchCharName, inMerch);
            rename(pchDataFileName, szDataFile);
        }
        if (fTempFile)
            rename(TEMP_DAT_FILE_NAME, pchDataFileName);
    }

    return (VS_SUCCESS);
}

/*-------------------------------------------------------------------------
    Function :    inCreateFieldPrintList
    Description : Goes through the configuration tables, and the data label 
    from all fields within the data record for the parameters report.
    Parameters :
    Returns :
              VS_ERR     on any error
              VS_SUCCESS on SUCCESS
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   10/26/95 T_Al_G     Created a new version.
      1   12/11/97 Lisa_W1    modified to print parameters report.
    --------------------------------------------------------------------------*/

int inCreateFieldPrintList(RecordList* prlRecordList,RecordNode* pndRecordNode,VS_BOOL *(*pfnFieldValidator) (FieldList *, int),FieldList** ppflFieldList) {
    int inNumberOfFields;
    int inResult = VS_FAILURE;
    FieldNode* pfnFieldNode;
    FieldList* pflLocalListPtr;
    FieldDefinition* psrFieldDefinitionPtr;

    /** /pdebug(("--inCreateFieldPrintList--"));*/
    *ppflFieldList = pflLocalListPtr = (FieldList *) pliCreateList((long)sizeof(FieldList));
    if (*ppflFieldList) {
        psrFieldDefinitionPtr = prlRecordList->psrFieldDefinitionPtr;
        pflLocalListPtr->prlParentRecord = prlRecordList;
        pflLocalListPtr->inRecordNumber = pndRecordNode->inRecordIndex;
        for (inNumberOfFields = 0; psrFieldDefinitionPtr[inNumberOfFields].ucFieldType != FLD_NULL; ++inNumberOfFields) {
            if ((pfnFieldValidator == NULL) || ((int) ((*pfnFieldValidator) (pflLocalListPtr, inNumberOfFields)) == VS_TRUE)) {
                pfnFieldNode = (FieldNode *) pndCreateNode((long)sizeof(FieldNode));
                if (pfnFieldNode) {
                    pfnFieldNode->inFieldIndex = inNumberOfFields;
                    if ((inResult = inAddToListEnd((List *) pflLocalListPtr, (Node *) pfnFieldNode)) != VS_SUCCESS) {
                        vdDestroyListAndNodes((List *) pflLocalListPtr);
                        *ppflFieldList = NULL_CH;
                        return(inResult);
                    }
                } else {
                    inResult = VS_ERR;      /*** This should generate an error condition!   ***/
                    return(inResult);
                }
            }
        }
        inResult = VS_SUCCESS;
    }

    return(inResult);
}


/************************************************************************************************
-------------------------------------------------------------------------
    Function :    inPrintRecord
    Description : Sets up the printing of each field within each record of 
    the config. tables for the parameters report.
    
    Parameters :
    Returns :
              VS_ERR     on any error
              VS_SUCCESS on SUCCESS
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   12/11/97 Lisa_W1    modified to print parameters report.
    --------------------------------------------------------------------------*/
int inPrintRecord(PRINTER_OBJECT* pobPrinter,RecordList* prlRecordList,Node* pndSelectedRecord) {
    int inResult = VS_FAILURE;
    int inSelectKey;
    FieldNode* pfnSelectedField;
    RecordList* pflFieldList;
    VS_BOOL *(*pfNull)() = 0;


    if (fValidList((List *) prlRecordList) && fValidNode((Node *) pndSelectedRecord)) {
        vdDisplayMessageFromFile(LAN_WAIT_ERR_MSG, STATUS_MSG); {
            if ((inResult = inCreateFieldPrintList(prlRecordList, (RecordNode *) pndSelectedRecord, pfNull, (FieldList * *) &pflFieldList)) == VS_SUCCESS) {
                pfnSelectedField = (FieldNode *) pndListStart((List *) pflFieldList);

                while ((inSelectKey = inSelectPrintNode(pobPrinter, (List *) pflFieldList, (Node * *) &pfnSelectedField, prlRecordList)) == KEY_CR) {
                    ;}

                vdDestroyListAndNodes((List *) pflFieldList);
            }
        }
    }
    return(inResult);
}


/*-------------------------------------------------------------------------
    Function :    inConfPrintRecs
    Description : Goes through the configuration tables, and prints the
    data label and data value for the parameters report.
    Parameters :
    Returns :
              VS_ERR     on any error
              VS_SUCCESS on SUCCESS
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   12/11/97 Lisa_W1    modified to print parameters report.
    --------------------------------------------------------------------------*/
int inConfPrintRecs(PRINTER_OBJECT* pobPrinter,char* pchDataFileName,char* pchPrintFileName) {
    long lnCondition = 0L;
    int inResult;
    Node* pndSelectedRecord;
    RecordList* prlRecordList;
    int inCounter = 0;
    char szTitle[50];
    FieldNode* pndFieldNode;
    char* chNull = 0;

    /* Don't reset szBuf - is a fmt used to print... */

    /**/pdebug(("--inConfPrintRecs--"));/**/

    if ((inResult = inCreateRecordList(pchDataFileName, &prlRecordList, NULL_PFI, -1, chNull)) >= VS_SUCCESS) {
        pndSelectedRecord = pndListStart((List *) prlRecordList);
        pndFieldNode = (FieldNode *) pndSelectedRecord;

        strcpy(srRcpt.szTransName, pchPrintFileName);
        pad(srRcpt.szTransName, srRcpt.szTransName, ' ', 16, CENTER);
        /*print the line*/
        lnCondition = 0x00000001;
        if (inPrintLines(pobPrinter, PARAM_TABLE_START, PARAM_TABLE_END, NO_BL_LINE, lnCondition) != VS_SUCCESS)
            return(VS_ERR);
        lnCondition = 0x00;

        while (pndSelectedRecord != NULL_CH) {
            if (inLoadConfRec(prlRecordList->pszFileName, prlRecordList->inRecordSize, inCounter, prlRecordList->pchRecordPtr) == VS_SUCCESS)
                vdGetFieldNodeValue(prlRecordList, prlRecordList->inTitleFieldIndex, szTitle);

            sprintf(srRcpt.szTransName, "Record %d", inCounter);
            /*print the current record line*/
            if (inPrintLines(pobPrinter, PARAM_TABLE_START, PARAM_TABLE_END, NO_BL_LINE, lnCondition) != VS_SUCCESS)
                return(VS_ERR);

            inResult = inPrintRecord(pobPrinter, prlRecordList, pndSelectedRecord);

            pndSelectedRecord = pndNextNode(pndSelectedRecord);
            inCounter++;
            if (act_kbd_pending_test(KEY_CANCEL)) {
                inResult = VS_ESCAPE;
                break;
            }
        }

        vdFree((char *) prlRecordList->psrFieldDefinitionPtr);
        vdFree(prlRecordList->pchRecordPtr);
        vdDestroyListAndNodes((List *) prlRecordList);
    }   
    return(inResult);
}
/**************************************************************************/
/*************************************************************************/

void vdPrintFieldNode(PRINTER_OBJECT* pobPrinter,FieldList* pflFieldList,FieldNode* pndFieldNode) {
    char szFieldValue[MAX_CONFIG_FIELD_SIZE + 1];
    char szFileName[FILENAME_SIZE + 1];
    long lnCondition = 0L;

    /** /pdebug(("--vdPrintFieldNode--"));*/

    /* Copy the first four characters from the filename 
        If it is a host data file the name will be four characters 
        If it is a configuration data file the name will be three characters
        So the decimal point in the fourth position will need to be removed
    */
    memset(szFileName, 0x00, sizeof(szFileName));
    strncpy(szFileName, pflFieldList->prlParentRecord->pszFileName, FILE_SUFFIX_SIZE);
    purge_char(szFileName, '.');

    /** /pdebug((szFileName ));*/


    /* Put field value into the printline var.*/
    vdGetFieldNodeValue(pflFieldList->prlParentRecord, pndFieldNode->inFieldIndex, szFieldValue);
    strcpy(srRcpt.szHeaderLine, szFieldValue);

    /* Put the field Label into the printline var.*/
    vdGetMessageFromThisFile(szFileName, pflFieldList->prlParentRecord->
                psrFieldDefinitionPtr[pndFieldNode->inFieldIndex].inFieldName, srRcpt.szTransName);

    /*print the line*/
    inPrintLines(pobPrinter, PARAM_FIELD_START, PARAM_FIELD_END, NO_BL_LINE, lnCondition);
}

/**************************************************************************/
/*-------------------------------------------------------------------------
    Function :    inSelectPrintNode
    Description : Prints the data label and data value for the parameters report.
    Parameters :
    Returns : inKey
              
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   12/11/97 Lisa_W1    modified inSelectNode to print parameters report.
    --------------------------------------------------------------------------*/

/*************************************************************************/
int inSelectPrintNode(PRINTER_OBJECT* pobPrinter,List* pliListOfChoices,Node** ppndSelectedNode,RecordList* prlRecordList) {
    Node* pndCurrentNode = *ppndSelectedNode;
    int inKey = 0;
    FieldDefinition* psrFieldDefinitionPtr;
    int inFieldNum = 0;

    /** /pdebug(("--inSelectPrintNode--"));
    pdebug(("ppndSelectedNode = %X", ppndSelectedNode));
    pdebug(("*ppndSelectedNode = %X", *ppndSelectedNode));
    pdebug(("**ppndSelectedNode = %X", **ppndSelectedNode));*/

    psrFieldDefinitionPtr = prlRecordList->psrFieldDefinitionPtr;

    while (1) {
        if (pndCurrentNode == NULL_CH) {
            inKey = KEY_BS;
            break;
        } else {
            if (prlRecordList->psrFieldDefinitionPtr[inFieldNum].fFieldPrint == 1)
                vdPrintFieldNode(pobPrinter, (FieldList *) pliListOfChoices, (FieldNode *) pndCurrentNode);

            inKey = KEY_CR;
        }
        inFieldNum++;
        pndCurrentNode = pndNextNode(pndCurrentNode);
        *ppndSelectedNode = pndCurrentNode;
    }

    return(inKey);
}


/*-------------------------------------------------------------------------
    Function :    inGetHostTRTFile
    Description : Goes through the configuration tables, and prints the
    data label and data value for the parameters report.
    Parameters :
    Returns :
              VS_ERR     on any error
              VS_SUCCESS on SUCCESS
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   01/23/98 Lisa_W1    modified to print TRT part of parameters report.
------------------------------------------------------------------------------*/
int inGetHostTRTFile(PRINTER_OBJECT* pobPrinter) {
    char szTempFile[FILENAME_SIZE + 1];
    long lnCondition = 0;
    int inNumRecs = 0;    
    int inCount = 0;
    int inResult = 0;

    inNumRecs = inGetHDTTotRecs();

    for (inCount = 0; inCount < inNumRecs; inCount++) {
        inLoadHDTRec(inCount);

        strcpy(szTempFile, szGetTRTFileName());
        //      TRT file name is just first name now        
        //      strlcpy(szDataFile,szTempFile,(strlen(szTempFile) - 4));

        if ((inResult = inTRTPrintRecs(pobPrinter, szTempFile)) != VS_SUCCESS)
            return(inResult);
    }


    /* Print extra linefeed before the tables sectionof report*/
    if (inPrintLines(pobPrinter, PARAM_TRT_NEWLINE, PARAM_TRT_NEWLINE, NO_BL_LINE, lnCondition) != VS_SUCCESS)
        inResult = VS_ERR;

    return(inResult);
}

/*-------------------------------------------------------------------------
    Function :    inTRTPrintRecs
    Description : Goes through the configuration tables, and prints the
    data label and data value for the parameters report.
    Parameters :
    Returns :
              VS_ERR     on any error
              VS_SUCCESS on SUCCESS
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   12/11/97 Lisa_W1    modified to print parameters report.
------------------------------------------------------------------------------*/
int inTRTPrintRecs(PRINTER_OBJECT* pobPrinter,char* pchDataFileName) {
    long lnCondition = 0L;
    int inResult = VS_SUCCESS;
    Node* pndSelectedRecord;
    RecordList* prlRecordList;
    int inCounter = 0;
    FieldNode* pndFieldNode;
    char* chNull = 0;

    /** /pdebug(("--inTRTPrintRecs--"));*/

    if ((inResult = inCreateRecordList(pchDataFileName, &prlRecordList, NULL_PFI, -1, chNull)) >= VS_SUCCESS) {
        pndSelectedRecord = pndListStart((List *) prlRecordList);
        pndFieldNode = (FieldNode *) pndSelectedRecord;

        /*print the filename line*/
        strcpy(srRcpt.szTransName, pchDataFileName);
        pad(srRcpt.szTransName, srRcpt.szTransName, ' ', 16, CENTER);
        lnCondition = 0x00000001;
        if (inPrintLines(pobPrinter, PARAM_TABLE_START, PARAM_TABLE_END, NO_BL_LINE, lnCondition) != VS_SUCCESS)
            return(VS_ERR);
        lnCondition = 0x00;

        while (pndSelectedRecord != NULL_CH) {
            inResult = inLoadConfRec(prlRecordList->pszFileName, prlRecordList->inRecordSize, inCounter, prlRecordList->pchRecordPtr);

            inResult = inPrintTRTRecord(pobPrinter, prlRecordList, pndSelectedRecord);
            pndSelectedRecord = pndNextNode(pndSelectedRecord);
            inCounter++;

            if (act_kbd_pending_test(KEY_CANCEL)) {
                inPrintLines(pobPrinter, PARAM_FORMFEED, PARAM_FORMFEED, NO_BL_LINE, lnCondition);
                inResult = VS_ESCAPE;
                break;
            }
        }

        vdFree((char *) prlRecordList->psrFieldDefinitionPtr);
        vdFree(prlRecordList->pchRecordPtr);
        vdDestroyListAndNodes((List *) prlRecordList);
    }
    return(inResult);
}
/************************************************************************************************
-------------------------------------------------------------------------
    Function :    inPrintTRTRecord
    Description : Sets up the printing of each field within each record of 
    the config. tables for the parameters report.
    
    Parameters :
    Returns :
              VS_ERR     on any error
              VS_SUCCESS on SUCCESS
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   12/11/97 Lisa_W1    modified to print parameters report.
    --------------------------------------------------------------------------*/
int inPrintTRTRecord(PRINTER_OBJECT* pobPrinter,RecordList* prlRecordList,Node* pndSelectedRecord) {
    int inResult = VS_FAILURE;
    int inSelectKey;
    FieldNode* pfnSelectedField;
    RecordList* pflFieldList;
    VS_BOOL *(*pfNull)() = 0;

    if (fValidList((List *) prlRecordList) && fValidNode((Node *) pndSelectedRecord)) {
        vdDisplayMessageFromFile(LAN_WAIT_ERR_MSG, STATUS_MSG); {
            if ((inResult = inCreateFieldPrintList(prlRecordList, (RecordNode *) pndSelectedRecord, pfNull, (FieldList * *) &pflFieldList)) == VS_SUCCESS) {
                pfnSelectedField = (FieldNode *) pndListStart((List *) pflFieldList);

                inSelectKey = inSelectTRTPrintNode(pobPrinter, (List *) pflFieldList, (Node * *) &pfnSelectedField, prlRecordList);

                vdDestroyListAndNodes((List *) pflFieldList);
            }
        }
    }
    return(inResult);
}


/**************************************************************************/
/*-------------------------------------------------------------------------
    Function :    inSelectTRTPrintNode
    Description : Prints the data label and data value for the parameters report.
    Parameters :
    Returns : inKey
              
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   12/11/97 Lisa_W1    modified inSelectNode to print parameters report.
    --------------------------------------------------------------------------*/

/*************************************************************************/
int inSelectTRTPrintNode(PRINTER_OBJECT* pobPrinter,List* pliListOfChoices,Node** ppndSelectedNode,RecordList* prlRecordList) {
    Node* pndCurrentNode = *ppndSelectedNode;
    int inKey = 0;
    FieldDefinition* psrFieldDefinitionPtr;
    int inFieldNum = 0;
    char szTableValue[PARAM_LINES_SIZE][PARAM_FIELDS_SIZE];
    int inParamLines = 0;
    int inParamFields = 0;
    long lnCondition = 0;

    /** /pdebug(("--inSelectTRTPrintNode--"));*/

    psrFieldDefinitionPtr = prlRecordList->psrFieldDefinitionPtr;

    memset(szTableValue, ' ', sizeof(szTableValue));

    while (1) {
        if (pndCurrentNode == NULL_CH) {
            inKey = KEY_BS;
            break;
        } else {
            if (prlRecordList->psrFieldDefinitionPtr[inFieldNum].fFieldPrint == 1)
                vdPrintTRTFieldNode(pobPrinter, (FieldList *) pliListOfChoices, (FieldNode *) pndCurrentNode, (char *) szTableValue, &inParamLines, (int *) &inParamFields);

            inKey = KEY_CR;
        }
        inFieldNum++;
        pndCurrentNode = pndNextNode(pndCurrentNode);
        *ppndSelectedNode = pndCurrentNode;
    }

    /*print the lines*/
    strlcpy(srRcpt.szRctDisclaimer1, szTableValue[0], 24);
    strlcpy(srRcpt.szRctDisclaimer2, szTableValue[1], 24);
    strlcpy(srRcpt.szRctDisclaimer3, szTableValue[2], 24);
    //strlcpy(srRcpt.szRctDisclaimer4,szTableValue[3], 24);
    inPrintLines(pobPrinter, PARAM_TRTFIELD_START, PARAM_TRTFIELD_END, NO_BL_LINE, lnCondition);

    return(inKey);
}

/*************************************************************************/

void vdPrintTRTFieldNode(PRINTER_OBJECT* pobPrinter,FieldList* pflFieldList,FieldNode* pndFieldNode,char* pszTableValue,int* pinParamLines,int* pinParamFields) {
    char szFieldValue[MAX_CONFIG_FIELD_SIZE + 1];

    char szTableValue[PARAM_LINES_SIZE][PARAM_FIELDS_SIZE];

    /** /pdebug(("--vdPrintTRTFieldNode--"));*/

    memcpy(szTableValue, pszTableValue, sizeof(szTableValue));

    /* Put field value into the printline var.*/
    vdGetSQAFieldNodeValue(pflFieldList->prlParentRecord, pndFieldNode->inFieldIndex, szFieldValue);

    if (pndFieldNode->inFieldIndex == 0)  /* Tran Code */ {
        strcpy(srRcpt.szHeaderLine2, szFieldValue);
        return;
    } else if (pndFieldNode->inFieldIndex == 1)  /* Tran Type */ {
        strcpy(srRcpt.szHeaderLine3, szFieldValue);
        return;
    } else if (pndFieldNode->inFieldIndex == 2)  /* Card Mode */ {
        strcpy(srRcpt.szHeaderLine4, szFieldValue);
        return;
    } else if (pndFieldNode->inFieldIndex == 3)  /* Tran Abbrev */ {
        strcpy(srRcpt.szTransName, szFieldValue);
        return;
    } else if (pndFieldNode->inFieldIndex == 4)  /* Banned Card */ {
        strcpy(srRcpt.szHeaderLine5, szFieldValue);
        return;
    } else if (pndFieldNode->inFieldIndex == 5)  /* Tip Proc */ {
        strcpy(srRcpt.szHeaderLine6, szFieldValue);
        return;
    }


    // char TranAbbrev[TRT_ABBREV_SIZE + 1];  /* Field 4      */
    if (pndFieldNode->inFieldIndex == 3) {
        strcpy(srRcpt.szTransName, szFieldValue);
        return;
    }

    if (*pinParamFields >= PARAM_FIELDS_SIZE) {
        *pinParamFields = 0;
        *pinParamLines += 1;
    }

    szTableValue[*pinParamLines][*pinParamFields] = szFieldValue[0];
    memcpy(pszTableValue, szTableValue, sizeof(szTableValue));

    *pinParamFields += 3;
}

/********************************************************************************************* 
* @func void | vdGetSQAFieldNodeValue | 
*       Creates a link list of the records in a data file
*
* @parm RecordList *|prlRecordList | 
*       The Link List of Configuration files
*
* @parm int | inFieldIndex | 
*       Index into the field table
*
* @parm char * |szFieldValue| 
*       Data value
*
* @end
**********************************************************************************************/

void vdGetSQAFieldNodeValue(RecordList* prlRecordList,int inFieldIndex,char* szFieldValue) {
    char szLocBuf[6 + 1];
    char* pchValuePtr;
    FieldDefinition* psrFieldDefinitionPtr;

    //pdebug(("--vdGetFieldNodeValue--"));
    /* pdebug(("inFieldIndex = %d", inFieldIndex)); */

    szFieldValue[0] = '\0';
    psrFieldDefinitionPtr = &prlRecordList->psrFieldDefinitionPtr[inFieldIndex];
    /** /pdebug(("inFieldPos = %d", psrFieldDefinitionPtr->inFieldPos));*/
    pchValuePtr = &prlRecordList->pchRecordPtr[psrFieldDefinitionPtr->inFieldPos];
    if ((psrFieldDefinitionPtr->chFieldIOFmt != INPUT_HIDE_NUMS) && (psrFieldDefinitionPtr->chFieldIOFmt != INPUT_HIDE_ALNUMS)) {
        /** /pdebug(("inFieldType = %X", psrFieldDefinitionPtr->ucFieldType));*/
        switch (psrFieldDefinitionPtr->ucFieldType) {
        case    FLD_CHAR:
            szFieldValue[0] = *pchValuePtr;
            szFieldValue[1] = '\0';
            break;

        case    FLD_BYTE:
            sprintf(szFieldValue, SUBSTITUTE_D, *(char *) pchValuePtr);
            break;

        case    FLD_SHORT:
            sprintf(szFieldValue, SUBSTITUTE_D, *(short *) pchValuePtr);
            break;

        case    FLD_INT:
            sprintf(szFieldValue, SUBSTITUTE_D, *(int *) pchValuePtr);
            break;

        case    FLD_LONG:
            sprintf(szFieldValue, SUBSTITUTE_LD, *(long *) pchValuePtr);
            break;

        case    FLD_DBL:
            sprintf(szFieldValue, "%g", *(double *) pchValuePtr);
            break;

        case    FLD_BIT:
            /** /pdebug(("inFieldBitNum = %d", psrFieldDefinitionPtr->inFieldBitNum));*/
            sprintf(szFieldValue, SUBSTITUTE_D, (*(int *) pchValuePtr & (1 << psrFieldDefinitionPtr->inFieldBitNum)) != 0);
            /*                
                            shBitFieldValue = *(short *)pchValuePtr & (1 << psrFieldDefinitionPtr->inFieldBitNum);
                            if (shBitFieldValue)
                                strcpy(szFieldValue, "ON");
                            else
                                strcpy(szFieldValue, "OFF");
            */                    
            break;

        case    FLD_STR:
            strlcpy(szFieldValue, pchValuePtr, psrFieldDefinitionPtr->inFieldSize);
            break;

        default:

                        ;
        }
    } else {
        strcpy(szLocBuf, "******");
        strcpy(szFieldValue, szLocBuf);
    }
    /** /pdebug(("szFieldValue = %s", szFieldValue));*/
}

int inGetHostHFTFile(PRINTER_OBJECT* pobPrinter) {
    char szDataFile[FILENAME_SIZE + 1];
    char szTempFile[FILENAME_SIZE + 1];
    long lnCondition = 0;
    int inNumRecs = 0;    
    int inCount = 0;
    int inResult = 0;

    inNumRecs = inGetHDTTotRecs();

    for (inCount = 0; inCount < inNumRecs; inCount++) {
        inLoadHDTRec(inCount);

        strcpy(szTempFile, szGetHostFileName());
        strlcpy(szDataFile, szTempFile, (strlen(szTempFile)));

        if ((inResult = inHFTPrintRecs(pobPrinter, szDataFile)) != VS_SUCCESS)
            return(inResult);
    }


    /* Print extra linefeed before the tables sectionof report*/
    (inPrintLines(pobPrinter, PARAM_TRT_NEWLINE, PARAM_TRT_NEWLINE, NO_BL_LINE, lnCondition));

    return(inResult);
}

int inHFTPrintRecs(PRINTER_OBJECT* pobPrinter,char* pchDataFileName) {
    long lnCondition = 0L;
    int inResult = VS_SUCCESS;
    Node* pndSelectedRecord;
    RecordList* prlRecordList;
    int inCounter = 0;
    FieldNode* pndFieldNode;
    char* chNull = 0;

    /** /pdebug(("--inHFTPrintRecs--"));*/

    if ((inResult = inCreateRecordList(pchDataFileName, &prlRecordList, NULL_PFI, -1, chNull)) >= VS_SUCCESS) {
        pndSelectedRecord = pndListStart((List *) prlRecordList);
        pndFieldNode = (FieldNode *) pndSelectedRecord;

        /*print the filename line*/
        strcpy(srRcpt.szTransName, pchDataFileName);
        pad(srRcpt.szTransName, srRcpt.szTransName, ' ', 16, CENTER);
        lnCondition = 0x00000001;
        if (inPrintLines(pobPrinter, PARAM_TABLE_START, PARAM_TABLE_END, NO_BL_LINE, lnCondition) != VS_SUCCESS)
            return(VS_ERR);
        lnCondition = 0x00;

        while (pndSelectedRecord != NULL_CH) {
            sprintf(srRcpt.szTransName, "Record %d", inCounter);
            /*print the current record line*/
            if (inPrintLines(pobPrinter, PARAM_TABLE_START, PARAM_TABLE_END, NO_BL_LINE, lnCondition) != VS_SUCCESS)
                return(VS_ERR);

            inResult = inLoadConfRec(prlRecordList->pszFileName, prlRecordList->inRecordSize, inCounter, prlRecordList->pchRecordPtr);

            inResult = inPrintHFTRecord(pobPrinter, prlRecordList, pndSelectedRecord);
            pndSelectedRecord = pndNextNode(pndSelectedRecord);
            inCounter++;

            if (act_kbd_pending_test(KEY_CANCEL)) {
                inPrintLines(pobPrinter, PARAM_FORMFEED, PARAM_FORMFEED, NO_BL_LINE, lnCondition);
                inResult = VS_ESCAPE;
                break;
            }
        }

        vdFree((char *) prlRecordList->psrFieldDefinitionPtr);
        vdFree(prlRecordList->pchRecordPtr);
        vdDestroyListAndNodes((List *) prlRecordList);
    }
    return(inResult);
}

int inPrintHFTRecord(PRINTER_OBJECT* pobPrinter,RecordList* prlRecordList,Node* pndSelectedRecord) {
    int inResult = VS_FAILURE;
    int inSelectKey;
    FieldNode* pfnSelectedField;
    RecordList* pflFieldList;
    VS_BOOL *(*pfNull)() = 0;

    if (fValidList((List *) prlRecordList) && fValidNode((Node *) pndSelectedRecord)) {
        vdDisplayMessageFromFile(LAN_WAIT_ERR_MSG, STATUS_MSG); {
            if ((inResult = inCreateFieldPrintList(prlRecordList, (RecordNode *) pndSelectedRecord, pfNull, (FieldList * *) &pflFieldList)) == VS_SUCCESS) {
                pfnSelectedField = (FieldNode *) pndListStart((List *) pflFieldList);

                inSelectKey = inSelectHFTPrintNode(pobPrinter, (List *) pflFieldList, (Node * *) &pfnSelectedField, prlRecordList);

                vdDestroyListAndNodes((List *) pflFieldList);
            }
        }
    }
    return(inResult);
}

int inSelectHFTPrintNode(PRINTER_OBJECT* pobPrinter,List* pliListOfChoices,Node** ppndSelectedNode,RecordList* prlRecordList) {
    Node* pndCurrentNode = *ppndSelectedNode;
    int inKey = 0;
    FieldDefinition* psrFieldDefinitionPtr;
    int inFieldNum = 0;
    char szTableValue[PARAM_LINES_SIZE][PARAM_FIELDS_SIZE];
    int inParamLines = 0;
    int inParamFields = 0;
    long lnCondition = 0;

    /** /pdebug(("--inSelectTRTPrintNode--"));*/

    psrFieldDefinitionPtr = prlRecordList->psrFieldDefinitionPtr;

    memset(szTableValue, ' ', sizeof(szTableValue));

    while (1) {
        if (pndCurrentNode == NULL_CH) {
            inKey = KEY_BS;
            break;
        } else {
            if (prlRecordList->psrFieldDefinitionPtr[inFieldNum].fFieldPrint == 1)
                vdPrintFieldNode(pobPrinter, (FieldList *) pliListOfChoices, (FieldNode *) pndCurrentNode);
            /*  if( prlRecordList->psrFieldDefinitionPtr[inFieldNum].fFieldPrint == 1)
                            vdPrintHFTFieldNode(pobPrinter, 
                                                (FieldList *)pliListOfChoices, 
                                                (FieldNode *)pndCurrentNode, 
                                                (char *)szTableValue,
                                                &inParamLines,
                                                (int *)&inParamFields);*/

            inKey = KEY_CR;
        }
        inFieldNum++;
        pndCurrentNode = pndNextNode(pndCurrentNode);
        *ppndSelectedNode = pndCurrentNode;
    }

    return(inKey);
}


void vdPrintHFTFieldNode(PRINTER_OBJECT* pobPrinter,FieldList* pflFieldList,FieldNode* pndFieldNode,char* pszTableValue,int* pinParamLines,int* pinParamFields) {
    char szFileName[FILENAME_SIZE + 1];
    long lnCondition = 0L;
    char szFieldValue[MAX_CONFIG_FIELD_SIZE + 1];

    char szTableValue[PARAM_LINES_SIZE][PARAM_FIELDS_SIZE];

    /** /pdebug(("--vdPrintTRTFieldNode--"));*/

    memcpy(szTableValue, pszTableValue, sizeof(szTableValue));

    /* Put field value into the printline var.*/
    vdGetSQAFieldNodeValue(pflFieldList->prlParentRecord, pndFieldNode->inFieldIndex, szFieldValue);

    /* Put field value into the printline var.*/
    vdGetFieldNodeValue(pflFieldList->prlParentRecord, pndFieldNode->inFieldIndex, szFieldValue);
    strcpy(srRcpt.szHeaderLine, szFieldValue);

    /* Put the field Label into the printline var.*/
    vdGetMessageFromThisFile(szFileName, pflFieldList->prlParentRecord->psrFieldDefinitionPtr[pndFieldNode->inFieldIndex].inFieldName, srRcpt.szTransName);

    /*print the line*/
    inPrintLines(pobPrinter, PARAM_FIELD_START, PARAM_FIELD_END, NO_BL_LINE, lnCondition);
}


#undef VSREPT3_C

